################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2014 Kevin Haroldsen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################

from .elements import ElementClass, alias_dict, element_info, element_transform


class BCDElement:

    def __init__(self, obj, type):
        self.obj = obj
        self._type = type
        self._changed = False
        self._identifier = None
        self._enum = None
        self._value = None
        self._type_info = element_info(self._type)
        self.fmt = self._type_info[1]

    def __hash__(self):
        if isinstance(self.value, list):
            return hash((self._type, tuple(self.value)))
        else:
            return hash((self._type, self.value))

    def __str__(self):
        return 'BCDElement<%s=%s>' % (self.identifier, str(self.value))

    def __repr__(self):
        return str(self)

    def _find_identifier(self):
        self._identifier = None
        self._enum = None

        cls, fmt, subtype = self._type_info

        v = None

        if cls == ElementClass.Application:
            v = alias_dict[cls][self.obj._type_info[2]].get(subtype)
        else:
            v = alias_dict[cls].get(subtype)

        if v is None:
            v = (fmt, 'custom:%x' % int(self._type, 16))
        elif len(v) == 3:
            self._enum = v[2]

        self._identifier = v[1]

    def _load_value(self):
        self._value = self.obj._nav.value('Element', path='Elements/' + self._type)

    @property
    def identifier(self):
        if self._identifier is None:
            self._find_identifier()
        return self._identifier

    @property
    def value(self):
        if self._value is None:
            self._load_value()

        return element_transform[self._type_info[1]][1](self._value)

    @value.setter
    def value(self, val):
        raise NotImplementedError('value setting not done yet')
        if self.name in element_transform:
            v = element_transform[self.name][0](self, val)
        else:
            v = val
        self._value = v
